package com.kaibes.module.verification;

import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaibes.core.base.core.VerificationImage;
import com.kaibes.core.base.util.VerificationImageUtils;

public class VerificationAction {

    public void drawNums(HttpSession session, HttpServletResponse response, String tag, int size) {
        VerificationImage image = VerificationImageUtils.drawNums(size);
        try {
            session.setAttribute(tag, image.getContent());
            response.setContentType("image/png");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            ImageIO.write(image.getImage(), "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void drawLetters(HttpSession session, HttpServletResponse response, String tag, int size) {
        VerificationImage image = VerificationImageUtils.drawLetters(size);
        try {
            session.setAttribute(tag, image.getContent());
            response.setContentType("image/png");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            ImageIO.write(image.getImage(), "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void drawChars(HttpSession session, HttpServletResponse response, String tag, int size) {
        VerificationImage image = VerificationImageUtils.drawChars(size);
        try {
            session.setAttribute(tag, image.getContent());
            response.setContentType("image/png");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            ImageIO.write(image.getImage(), "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
